package com.gerfmarquez.nymph.db.sqlite;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.gerfmarquez.nymph.db.sqlite.dto.Song;

public class SongDbAdapter {
	public static final String KEY_ID_SONG = "id_song";
	public static final String KEY_SONG_NAME = "name";
	public static final String KEY_INSTRUMENT = "id_instrument";
	private static final String SONG_TABLE = "song";
	
	private SQLiteDatabase database;
	
	public SongDbAdapter(SQLiteDatabase database) {
		this.database = database;
	}
	

	public long createSong(String category, int instrument) {
		ContentValues initialValues = createContentValues(category, instrument);
		long rowId = database.insert(SONG_TABLE, null, initialValues);
		return rowId;
	}

	
/** * Update the todo */

	public boolean updateTodo(long rowId, String category, String summary,
			String description) {
//		ContentValues updateValues = createContentValues(category, summary,
//				description);
//
//		return database.update(SONG_TABLE, updateValues, KEY_ROWID + "="
//				+ rowId, null) > 0;
		return false;
	}

	
/** * Deletes todo */

	public boolean deleteTodo(long rowId) {
//		return database.delete(SONG_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
		return false;
	}

	
/** * Return a Cursor over the list of all todo in the database * * @return Cursor over all notes */

	public ArrayList<Song> fetchAllSongs() {
		Cursor cursor = database.query(SONG_TABLE, new String[] { KEY_ID_SONG,
				KEY_SONG_NAME, KEY_INSTRUMENT }, null, null, null,
				null, null);
		ArrayList<Song> songs = new ArrayList<Song>();
		if(cursor != null) {
			cursor.moveToFirst();
			while(!cursor.isAfterLast()) {
				Song songTemp = new Song();
				songTemp.setIdSong(cursor.getInt(cursor.getColumnIndex(KEY_ID_SONG)));
				songTemp.setSongName(cursor.getString(cursor.getColumnIndex(KEY_SONG_NAME)));
				songs.add(songTemp);
				cursor.moveToNext();
			}
		}
		return songs;
	}

	
/** * Return a Cursor positioned at the defined todo */

//	public Cursor fetchTodo(long rowId) throws SQLException {
//		Cursor mCursor = database.query(true, SONG_TABLE, new String[] {
//				KEY_ROWID, KEY_CATEGORY, KEY_SUMMARY, KEY_DESCRIPTION },
//				KEY_ROWID + "=" + rowId, null, null, null, null, null);
//		if (mCursor != null) {
//			mCursor.moveToFirst();
//		}
//		return mCursor;
//	}

	private ContentValues createContentValues(String songName, int instrument) {
		ContentValues values = new ContentValues();
		values.put(KEY_SONG_NAME, songName);
		values.put(KEY_INSTRUMENT, instrument);
		return values;
	}
}
